package com.learning.jquizenglish.screen.more;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.common.adapter.BaseListAdapter;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.ProfileModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.network.JQuizEnglishApi.Level;
import com.learning.jquizenglish.screen.profile.ProfileScreen;
import com.learning.jquizenglish.utility.DialogUtil;
import com.learning.jquizenglish.widget.ViewHolder;
import com.learning.jquizenglish.widget.smoothprogressbar.SmoothProgressBar;
import com.nostra13.universalimageloader.core.ImageLoader;

public class HighScoreScreen extends JQuizEnglishFragment implements OnItemClickListener{

	private Spinner spPack;
	private ListView listViewHighScore;
	private SmoothProgressBar mSmoothProgressBar;
	private Level highScoreLevel;
	private ArrayList<ProfileModel> mListData;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.screen_high_scrore, container,false);
//		spPack = (Spinner) v.findViewById(R.id.spPack);
		mSmoothProgressBar = (SmoothProgressBar) v.findViewById(R.id.progressbar);
		listViewHighScore = (ListView) v.findViewById(R.id.list_highscore);
		listViewHighScore.setOnItemClickListener(this);
//		initData();
		requestData();
		return v;
	}
	
	private void initData(){
		final ArrayList<String> arrHighScoreLevel = new ArrayList<String>();
		arrHighScoreLevel.add("English Grammar: Elementary");
		arrHighScoreLevel.add("English Grammar: Intermediate");
		arrHighScoreLevel.add("English Grammar: Advanced");
	    ArrayAdapter<String> spAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_black, arrHighScoreLevel);
	    spAdapter.setDropDownViewResource(R.layout.spinner_item_black);
	    spPack.setAdapter(spAdapter);
	    spPack.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int pos, long arg3) {
				switch (pos) {
				case 0:
					highScoreLevel = Level.ELEMENTARY;
					break;
				case 1:
					highScoreLevel = Level.INTERMEDIATE;
					break;
				case 2:
					highScoreLevel = Level.ADVANCED;
					break;
				default:
					highScoreLevel = Level.ELEMENTARY;
					break;
				}
				requestData();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	private void requestData(){
		JQuizRequestModel requestModel = JQuizEnglishApi.getRequestListHighScore();
		executeGetTaskReturnList(requestModel, false, ProfileModel.class, new JQuizTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if (object!=null) {
					mListData = (ArrayList<ProfileModel>) object;
					HighScoreAdapter adapter = new HighScoreAdapter(getActivity(), mListData);
					listViewHighScore.setAdapter(adapter);
				}
				mSmoothProgressBar.setVisibility(View.GONE);
			}
		});
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		DialogUtil.showUserInfoDialog(getActivity(), mListData.get(position));
	}
	
	class HighScoreAdapter extends BaseListAdapter<ProfileModel>{

		public HighScoreAdapter(Activity activity, ArrayList<ProfileModel> datas) {
			super(activity, datas);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.high_score_item, null);
			}
			
			ImageView imgAvatar = ViewHolder.get(convertView, R.id.imgAvatar);
			TextView txtName = ViewHolder.get(convertView, R.id.txtName);
			TextView txtScore = ViewHolder.get(convertView, R.id.txtScore);
			ProfileModel model = getItem(position);
			if (model!=null) {
				txtName.setText(model.name);
				txtScore.setText(""+model.total_score+" points");
				if (model.avatar!=null && !model.avatar.equals("")) {
					ImageLoader.getInstance().displayImage(JQuizEnglishApi.BASE_IMAGE_URL+model.avatar, imgAvatar,ProfileScreen.options);
				}
			}
			return convertView;
		}
		
	}

}
